package com.lyq.permission.controller;

import com.lyq.annotation.LYQPermission;
import com.lyq.permission.domain.Role;
import com.lyq.permission.dto.EmpRoleDTO;
import com.lyq.permission.dto.RolePermissionDTO;
import com.lyq.permission.query.RoleQuery;
import com.lyq.permission.service.IRoleService;
import com.lyq.basic.utils.PageList;
import com.lyq.basic.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author:liangyunqian
 * @Date: 2022/12/14  11:39
 * @Description:
 */
@RestController
@RequestMapping("/role")
@LYQPermission(name = "角色管理")
@Api(value = "角色的Api",description = "角色的增删改查")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    //添加和修改
    @LYQPermission(name = "添加和修改角色")
    @PutMapping
    @ApiOperation(value = "添加和修改角色")
    public R addOrUpdateRole(@RequestBody Role role){
        try {
            if (Objects.isNull(role.getId())){
                roleService.insert(role);
            }else {
                roleService.update(role);
            }
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("保存失败"+e.getMessage());
        }
    }

    //删除单个
    @ApiOperation("删除单个角色")
    @LYQPermission(name = "删除单个角色")
    @DeleteMapping("/{id}")
    public R deleteRole(@PathVariable("id")Long id){
        try {
            roleService.deleteById(id);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }
    //批量删除
    @ApiOperation("批量删除角色")
    @LYQPermission(name = "批量删除角色")
    @PatchMapping
    public R batchDeleteRole(@RequestBody List<Long> ids){
        try {
            roleService.batchDelete(ids);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("批量删除失败!"+e.getMessage());
        }
    }
    //根据id查询
    @ApiOperation("查询单个角色")
    @LYQPermission(name = "查询单个角色")
    @GetMapping("/{id}")
    public R selectRole(@PathVariable("id")Long id){
        try {
            Role role = roleService.getById(id);
            return R.me().setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }
    //查询所有
    @ApiOperation("查询所有角色")
    @LYQPermission(name = "查询所有角色")
    @GetMapping
    public R selectAll(){
        try {
            List<Role> roles = roleService.getAll();
            return R.me().setData(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    //分页查询和高级查询
    @ApiOperation("分页查询和高级查询角色")
    @LYQPermission(name = "分页查询和高级查询角色")
    @PostMapping
    public R listPage(@RequestBody RoleQuery query){
        try {
            PageList<Role> pageList = roleService.listPage(query);
            return R.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    @LYQPermission(name = "设置权限")
    @PutMapping("/permission")
    public R saveRolePermission(@RequestBody RolePermissionDTO dto){
        try {
            roleService.saveRolePermission(dto);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }

    @LYQPermission(name = "获取角色的权限")
    @GetMapping("/permissionsns/{roleId}")
    public R getRolePermissionSns(@PathVariable("roleId")Long roleId){
        try {
            List<String> sns = roleService.getRolePermissionSns(roleId);
            return R.me().setData(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("获取角色的权限失败!"+e.getMessage());
        }
    }

    @LYQPermission(name = "设置员工角色")
    @PutMapping("/empRole")
    public R saveEmpRole(@RequestBody EmpRoleDTO dto){
        try {
            roleService.saveEmpRole(dto);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("设置员工角色失败!"+e.getMessage());
        }
    }

    @LYQPermission(name = "查询员工角色")
    @GetMapping("/getEmpRole/{id}")
    public R getEmpRole(@PathVariable("id") Long empId){
        try {
            EmpRoleDTO vo = roleService.getEmpRole(empId);
            if (!Objects.isNull(vo)){
                return R.me().setData(vo);
            }
            return R.me().setSuccess(false).setMessage("该员工未设置角色");
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("查询员工角色失败!"+e.getMessage());
        }
    }
}
